package com.sleepcamel.exporter.model;

import java.util.Arrays;

import com.sleepcamel.rest.domain.JavaClass;
import com.sleepcamel.rest.domain.JavaProperty;

import groovy.transform.ToString;

@ToString(includeNames=true,includeFields=true,excludes="singularName,owner,ignore,otherSide,bidirectional")
class Relationship{
	String name
	String singularName
	RelationshipType type
	Key key
	JavaDBClass owner
	JavaDBClass relatedClass
	boolean bidirectional=true
	boolean ignore=false
	Relationship otherSide


    def validate() {
        assert name : "name is not null"
        assert type : "type is not null"
        assert owner: "owner is not null"
        assert relatedClass : "relatedClass is not null"
        if (bidirectional) {
            assert otherSide : "other side is not null"
        }
    }
	
	public JavaProperty toJavaProperty(){
		JavaProperty javaProperty = new JavaProperty();
		javaProperty.setName(name);
		JavaClass javaClass = new JavaClass();
		javaClass.setName(relatedClass.name);
		if ( !type.equals(RelationshipType.MANY_TO_ONE) ){
			javaClass = javaClass.asSet();
		}
		javaProperty.setJavaClass(javaClass)
		return javaProperty;
	}
}